package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.webUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 14587
 * @date 2020/10/22 22:26
 */
public class ClientBookServlet extends BaseServlet{
    private BookService bookService=new BookServiceImpl();
/**
 * @author VegetDog
 * @Description 处理分页功能
 * @date 2020-10-23 00:06
 * @param req
 * @param resp
 * @return void
 */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求参数pageNo和pageSize,这里没传入，默认是1
        int pageNo = webUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = webUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用bookService.page(pageNo,pageSize)方法：page对象
        Page<Book> page=bookService.page(pageNo,pageSize);
        page.setUrl("client/bookServlet?action=page");
        //3.保存page对象到request域中
        req.setAttribute("page",page);
        //4.请求转发到/pages/manager/book_manager.jsp下
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }
/**
 * @author VegetDog
 * @Description 根据价格处理分页功能
 * @date 2020-10-23 00:06
 * @param req
 * @param resp
 * @return void
 */
    protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求参数pageNo和pageSize,min,max,这里没传入，默认是1
        int pageNo = webUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = webUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        int max=webUtils.parseInt(req.getParameter("max"),Integer.MAX_VALUE);
        int min=webUtils.parseInt(req.getParameter("min"),0);
        //2.调用bookService.page(pageNo,pageSize)方法：page对象
        Page<Book> page=bookService.pageByPrice(pageNo,pageSize,min,max);
        StringBuilder sb=new StringBuilder("client/bookServlet?action=pageByPrice");
        if (req.getParameter("min")!=null){
            sb.append("&min="+req.getParameter("min"));
        }
        if (req.getParameter("max")!=null){
            sb.append("&max="+req.getParameter("max"));
        }
        page.setUrl(sb.toString());
        //3.保存page对象到request域中
        req.setAttribute("page",page);
        //4.请求转发到/pages/manager/book_manager.jsp下
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }
}
